package com.ruoyi.web.controller.city.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.city.domain.CmsCity;
import com.ruoyi.web.controller.city.mapper.CmsCityMapper;
import com.ruoyi.web.controller.city.service.ICmsCityService;

/**
 * 城市Service业务层处理
 *
 * @author ruoyi
 * @date 2020-10-09
 */
@Service
public class CmsCityServiceImpl implements ICmsCityService
{
    @Autowired
    private CmsCityMapper cmsCityMapper;

    /**
     * 查询城市
     *
     * @param id 城市ID
     * @return 城市
     */
    @Override
    public CmsCity selectCmsCityById(Long id)
    {
        return cmsCityMapper.selectCmsCityById(id);
    }

    @Override
    public CmsCity selectCmsCityByName(Map<String,Object> map) {
        return cmsCityMapper.selectCmsCityByName(map);
    }

    /**
     * 查询城市列表
     *
     * @param cmsCity 城市
     * @return 城市
     */
    @Override
    public List<CmsCity> selectCmsCityList(CmsCity cmsCity)
    {
        return cmsCityMapper.selectCmsCityList(cmsCity);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param citys 部门列表
     * @return 树结构列表
     */
    @Override
    public List<CmsCity> buildCityTree(List<CmsCity> citys)
    {
        List<CmsCity> returnList = new ArrayList<CmsCity>();
        List<Long> tempList = new ArrayList<Long>();
        for (CmsCity city : citys)
        {
            tempList.add(city.getId());
        }
        for (Iterator<CmsCity> iterator = citys.iterator(); iterator.hasNext();)
        {
            CmsCity city = (CmsCity) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(city.getParentId()))
            {
                recursionFn(citys, city);
                returnList.add(city);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = citys;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildCityTreeSelect(List<CmsCity> citys) {
        List<CmsCity> cityTrees = buildCityTree(citys);
//        return cityTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
        return null;
    }

    @Override
    public List<CmsCity> queryList() {
        return cmsCityMapper.queryList();
    }

    @Override
    public List<Map<String,Object>> queryByParam(Map<String, Object> map) {
        return cmsCityMapper.queryByParam(map);
    }

    /**
     * 新增城市
     *
     * @param cmsCity 城市
     * @return 结果
     */
    @Override
    public int insertCmsCity(CmsCity cmsCity)
    {
        return cmsCityMapper.insertCmsCity(cmsCity);
    }

    /**
     * 修改城市
     *
     * @param cmsCity 城市
     * @return 结果
     */
    @Override
    public int updateCmsCity(CmsCity cmsCity)
    {
        return cmsCityMapper.updateCmsCity(cmsCity);
    }

    /**
     * 批量删除城市
     *
     * @param ids 需要删除的城市ID
     * @return 结果
     */
    @Override
    public int deleteCmsCityByIds(Long[] ids)
    {
        return cmsCityMapper.deleteCmsCityByIds(ids);
    }

    /**
     * 删除城市信息
     *
     * @param id 城市ID
     * @return 结果
     */
    @Override
    public int deleteCmsCityById(Long id)
    {
        return cmsCityMapper.deleteCmsCityById(id);
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<CmsCity> list, CmsCity t)
    {
        // 得到子节点列表
        List<CmsCity> childList = getChildList(list, t);
        t.setChildren(childList);
        for (CmsCity tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                // 判断是否有子节点
                Iterator<CmsCity> it = childList.iterator();
                while (it.hasNext())
                {
                    CmsCity n = (CmsCity) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<CmsCity> getChildList(List<CmsCity> list, CmsCity t)
    {
        List<CmsCity> tlist = new ArrayList<CmsCity>();
        Iterator<CmsCity> it = list.iterator();
        while (it.hasNext())
        {
            CmsCity n = (CmsCity) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<CmsCity> list, CmsCity t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}